package org.shreker.shan.website.business.controller;

import org.shreker.shan.core.domain.Account;
import org.shreker.shan.core.domain.User;
import org.shreker.shan.core.service.IAccountService;
import org.shreker.shan.core.service.IUserService;
import org.shreker.shan.core.util.LoginContext;
import org.shreker.shan.core.util.ResultJSON;
import org.shreker.shan.rbac.annotation.PermissionRequired;
import org.shreker.shan.website.base.controller.WebSiteController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MineController extends WebSiteController {

	@Autowired
	private IAccountService accountService;
	
	@Autowired
	private IUserService userService;
	
	@PermissionRequired("个人首页")
	@RequestMapping("/personal")
	public String personal(Model model) {
		try {
			Account account = accountService.get(LoginContext.getCurrentLogin().getId());
			model.addAttribute("account", account);
			User user = userService.get(LoginContext.getCurrentLogin().getId());
			model.addAttribute("user", user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "personal";
	}
	
	@RequestMapping("/sendVerifyCode")
	@ResponseBody
	public ResultJSON sendVerifyCode(Model model, String phone) {
		ResultJSON json = new ResultJSON();
		try {
			userService.sendVerifyCode(phone);
			json.setSuccess(true);
			json.setInfo("验证码发送成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.setSuccess(false);
			json.setInfo(e.getMessage());
		}
		return json;
	}
	
	/**
	 * 验证手机号码
	 * @param model
	 * @param phone
	 * @return
	 */
	@RequestMapping("/verifyPhone")
	@ResponseBody
	public ResultJSON verifyPhone(Model model, String phone, String code) {
		ResultJSON json = new ResultJSON();
		try {
			userService.verifyPhone(phone, code);
			json.setSuccess(true);
			json.setInfo("手机验证成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.setSuccess(false);
			json.setInfo(e.getMessage());
		}
		return json;
	}

}
